package com.hsnn.medstgmini.pro.base.service.Impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hsnn.medstgmini.pro.base.dao.AnaHospDao;
import com.hsnn.medstgmini.pro.base.service.AnaHospManager;
import com.hsnn.medstgmini.util.Pagination;
import com.hsnn.medstgmini.util.ParseNumber;

/**
 * @category 医院端统计分析应用层实现类
 * @author lil
 * @date 2015年12月07日
 */
@Service
public class AnaHospManagerImpl implements AnaHospManager {
	@Autowired
	private AnaHospDao anaHospDao;
	
	/**
	 * @category 获取药品采购总量日汇总数据
	 * @date 2015年12月07日
	 * @author lil
	 * @param page
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Pagination getAnaListByTime(Pagination page) {
		
		//获取药品采购总量日汇总数据
		Pagination anaTotal = anaHospDao.getAnaListByTime(page);
		List<Map<String, Object>> listPage = (List<Map<String, Object>>) page.getRows();
		if (null != listPage && listPage.size() > 0){
			for (int i = 0; i < listPage.size(); i++){
				Map<String, Object> map = listPage.get(i);
				map.put("total_amount", map.get("total_amount")==null?"":ParseNumber.toStrDecimal(ParseNumber.toDouble(map.get("total_amount").toString()), 3));
				map.put("del_amount", map.get("del_amount")==null?"":ParseNumber.toStrDecimal(ParseNumber.toDouble(map.get("del_amount").toString()), 3));
				map.put("ret_amount", map.get("ret_amount")==null?"":ParseNumber.toStrDecimal(ParseNumber.toDouble(map.get("ret_amount").toString()), 3));
				map.put("del_ratio", map.get("del_ratio")==null?"":ParseNumber.toStrDecimal(ParseNumber.toDouble(map.get("del_ratio").toString()) * 100, 6) + "%");
				map.put("ret_ratio", map.get("ret_ratio")==null?"":ParseNumber.toStrDecimal(ParseNumber.toDouble(map.get("ret_ratio").toString()) * 100, 6) + "%");
				map.put("base_ratio", map.get("base_ratio")==null?"":ParseNumber.toStrDecimal(ParseNumber.toDouble(map.get("base_ratio").toString()) * 100, 6) + "%");
			}
		}
		
		
		//获取药品采购总量日汇总集合
		List<Map<String, Object>> list = anaHospDao.getAnaChartByTime(page.getConditions().get("hospCode"));
		String yTime = "[";//图表y轴时间
		String total = "[";//采购金额
		String del = "[";//配送金额
		String ret = "[";//退货金额
		//循环解析数据
		for(int i =0;i<list.size();i++) {
			
			Map<String, Object> map = list.get(i);
			String date = map.get("date")+"";
			yTime = yTime +"'"+date+ "',";
			total = total + map.get("total_amount") + ",";
			del = del + map.get("del_amount") + ",";
			ret = ret + map.get("ret_amount") + ",";
		}
		
		if(!"[".equals(yTime)) {//判断是否为空
			yTime = yTime.substring(0, yTime.length()-1) + "]";
			total = total.substring(0, total.length()-1) + "]";
			del = del.substring(0, del.length()-1) + "]";
			ret = ret.substring(0, ret.length()-1) + "]";
			
			anaTotal.getConditions().put("yTime", yTime);
			anaTotal.getConditions().put("total", total);
			anaTotal.getConditions().put("del", del);
			anaTotal.getConditions().put("ret", ret);
		}
		
		return anaTotal;
	}
	
	/**
	 * @category 获取药品采购总量月汇总数据
	 * @date 2015年12月07日
	 * @author lil
	 * @param page
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Pagination getAnaListByTimeMonth(Pagination page) {
		
		//获取药品采购总量月汇总数据
		Pagination anaTotal = anaHospDao.getAnaListByTimeMonth(page);
		List<Map<String, Object>> listPage = (List<Map<String, Object>>) page.getRows();
		if (null != listPage && listPage.size() > 0){
			for (int i = 0; i < listPage.size(); i++){
				Map<String, Object> map = listPage.get(i);
				map.put("total_amount", map.get("total_amount")==null?"":ParseNumber.toStrDecimal(ParseNumber.toDouble(map.get("total_amount").toString()), 3));
				map.put("del_amount", map.get("del_amount")==null?"":ParseNumber.toStrDecimal(ParseNumber.toDouble(map.get("del_amount").toString()), 3));
				map.put("ret_amount", map.get("ret_amount")==null?"":ParseNumber.toStrDecimal(ParseNumber.toDouble(map.get("ret_amount").toString()), 3));
				map.put("del_ratio", map.get("del_ratio")==null?"":ParseNumber.toStrDecimal(ParseNumber.toDouble(map.get("del_ratio").toString()) * 100, 6) + "%");
				map.put("ret_ratio", map.get("ret_ratio")==null?"":ParseNumber.toStrDecimal(ParseNumber.toDouble(map.get("ret_ratio").toString()) * 100, 6) + "%");
				map.put("base_ratio", map.get("base_ratio")==null?"":ParseNumber.toStrDecimal(ParseNumber.toDouble(map.get("base_ratio").toString()) * 100, 6) + "%");
			}
		}
		
		//获取药品采购总量月汇总集合
		List<Map<String, Object>> list = anaHospDao.getAnaChartByTimeMon(page.getConditions().get("hospCode"));
		String yTime = "[";//图表y轴时间
		String total = "[";//采购金额
		String del = "[";//配送金额
		String ret = "[";//退货金额
		//循环解析数据
		for(int i =0;i<list.size();i++) {
			Map<String, Object> map = list.get(i);
			String date = map.get("date")+"";
			yTime = yTime +"'"+date+ "',";
			total = total + map.get("total_amount") + ",";
			del = del + map.get("del_amount") + ",";
			ret = ret + map.get("ret_amount") + ",";
		}
		
		if(!"[".equals(yTime)) {//判断是否为空
			yTime = yTime.substring(0, yTime.length()-1) + "]";
			total = total.substring(0, total.length()-1) + "]";
			del = del.substring(0, del.length()-1) + "]";
			ret = ret.substring(0, ret.length()-1) + "]";
			
			anaTotal.getConditions().put("yTime", yTime);
			anaTotal.getConditions().put("total", total);
			anaTotal.getConditions().put("del", del);
			anaTotal.getConditions().put("ret", ret);
		}
		
		return anaTotal;
	}
	
	/**
	 * @category 获取药品采购总量季度汇总数据
	 * @date 2015年12月07日
	 * @author lil
	 * @param page
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Pagination getAnaListByTimeQuarter(Pagination page) {
		
		//获取药品采购总量季度汇总数据
		Pagination anaTotal = anaHospDao.getAnaListByTimeQuarter(page);
		List<Map<String, Object>> listPage = (List<Map<String, Object>>) page.getRows();
		if (null != listPage && listPage.size() > 0){
			for (int i = 0; i < listPage.size(); i++){
				Map<String, Object> map = listPage.get(i);
				map.put("total_amount", map.get("total_amount")==null?"":ParseNumber.toStrDecimal(ParseNumber.toDouble(map.get("total_amount").toString()), 3));
				map.put("del_amount", map.get("del_amount")==null?"":ParseNumber.toStrDecimal(ParseNumber.toDouble(map.get("del_amount").toString()), 3));
				map.put("ret_amount", map.get("ret_amount")==null?"":ParseNumber.toStrDecimal(ParseNumber.toDouble(map.get("ret_amount").toString()), 3));
				map.put("del_ratio", map.get("del_ratio")==null?"":ParseNumber.toStrDecimal(ParseNumber.toDouble(map.get("del_ratio").toString()) * 100, 6) + "%");
				map.put("ret_ratio", map.get("ret_ratio")==null?"":ParseNumber.toStrDecimal(ParseNumber.toDouble(map.get("ret_ratio").toString()) * 100, 6) + "%");
				map.put("base_ratio", map.get("base_ratio")==null?"":ParseNumber.toStrDecimal(ParseNumber.toDouble(map.get("base_ratio").toString()) * 100, 6) + "%");
			}
		}
		
		//获取药品采购总量季度汇总集合
		List<Map<String, Object>> list = anaHospDao.getAnaChartByTimeSea(page.getConditions().get("hospCode"));
		String yTime = "[";//图表y轴时间
		String total = "[";//采购金额
		String del = "[";//配送金额
		String ret = "[";//退货金额
		//循环解析数据
		for(int i =0;i<list.size();i++) {
			Map<String, Object> map = list.get(i);
			String date = map.get("date")+"";
			yTime = yTime +"'"+date+ "',";
			total = total + map.get("total_amount") + ",";
			del = del + map.get("del_amount") + ",";
			ret = ret + map.get("ret_amount") + ",";
		}
		
		if(!"[".equals(yTime)) {//判断是否为空
			yTime = yTime.substring(0, yTime.length()-1) + "]";
			total = total.substring(0, total.length()-1) + "]";
			del = del.substring(0, del.length()-1) + "]";
			ret = ret.substring(0, ret.length()-1) + "]";
			
			anaTotal.getConditions().put("yTime", yTime);
			anaTotal.getConditions().put("total", total);
			anaTotal.getConditions().put("del", del);
			anaTotal.getConditions().put("ret", ret);
		}
		
		return anaTotal;
	}
	
	/**
	 * @category 获取药品采购总量年汇总数据
	 * @date 2015年12月07日
	 * @author lil
	 * @param page
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Pagination getAnaListByTimeYear(Pagination page) {
		
		//获取药品采购总量年汇总数据
		Pagination anaTotal = anaHospDao.getAnaListByTimeYear(page);
		List<Map<String, Object>> listPage = (List<Map<String, Object>>) page.getRows();
		if (null != listPage && listPage.size() > 0){
			for (int i = 0; i < listPage.size(); i++){
				Map<String, Object> map = listPage.get(i);
				map.put("total_amount", map.get("total_amount")==null?"":ParseNumber.toStrDecimal(ParseNumber.toDouble(map.get("total_amount").toString()), 3));
				map.put("del_amount", map.get("del_amount")==null?"":ParseNumber.toStrDecimal(ParseNumber.toDouble(map.get("del_amount").toString()), 3));
				map.put("ret_amount", map.get("ret_amount")==null?"":ParseNumber.toStrDecimal(ParseNumber.toDouble(map.get("ret_amount").toString()), 3));
				map.put("del_ratio", map.get("del_ratio")==null?"":ParseNumber.toStrDecimal(ParseNumber.toDouble(map.get("del_ratio").toString()) * 100, 6) + "%");
				map.put("ret_ratio", map.get("ret_ratio")==null?"":ParseNumber.toStrDecimal(ParseNumber.toDouble(map.get("ret_ratio").toString()) * 100, 6) + "%");
				map.put("base_ratio", map.get("base_ratio")==null?"":ParseNumber.toStrDecimal(ParseNumber.toDouble(map.get("base_ratio").toString()) * 100, 6) + "%");
			}
		}
		
		
		//获取药品采购总量年汇总集合
		List<Map<String, Object>> list = anaHospDao.getAnaChartByTimeYear(page.getConditions().get("hospCode"));
		String yTime = "[";//图表y轴时间
		String total = "[";//采购金额
		String del = "[";//配送金额
		String ret = "[";//退货金额
		//循环解析数据
		for(int i =0;i<list.size();i++) {
			Map<String, Object> map = list.get(i);
			String date = map.get("date")+"";
			yTime = yTime +"'"+date+ "',";
			total = total + map.get("total_amount") + ",";
			del = del + map.get("del_amount") + ",";
			ret = ret + map.get("ret_amount") + ",";
		}
		
		if(!"[".equals(yTime)) {//判断是否为空
			yTime = yTime.substring(0, yTime.length()-1) + "]";
			total = total.substring(0, total.length()-1) + "]";
			del = del.substring(0, del.length()-1) + "]";
			ret = ret.substring(0, ret.length()-1) + "]";
			
			anaTotal.getConditions().put("yTime", yTime);
			anaTotal.getConditions().put("total", total);
			anaTotal.getConditions().put("del", del);
			anaTotal.getConditions().put("ret", ret);
		}
		
		return anaTotal;
	}
	
}
